package com.mystudy.leetcode.problem.string.p_383;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: infoalgorithm
 * @description: 赎金信
 * @author: zhouzhilong
 * @create: 2019-07-11 13:41
 **/
public class Solution {
    /**
     * logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(Solution.class);

    @Test
    public void test() {
        boolean can = canConstruct("aa", "aab");
        LOGGER.debug("can = [{}]",can);

    }


    /**
     * 维护一个数组的解法　时间复杂度:O(n),非常强
     *
     * @param ransomNote
     * @param magazine
     * @return
     */
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] counts = new int[26];
        char[] ransomChars = magazine.toCharArray();
        for (char c : ransomChars) {
            counts[c - 'a']++;
        }
        char[] magazineChars = ransomNote.toCharArray();
        for (char c : magazineChars) {
            if (counts[c - 'a'] == 0) {
                return false;
            }
            counts[c - 'a']--;
        }

        return true;
    }

}
